import json

import scrapy
from fake_useragent import UserAgent


ua = UserAgent().random
class AllDramaSpider(scrapy.Spider):
    name = "all_drama"

    def start_requests(self):
        urls = [f"https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page={page}&season_type=1&pagesize=20&type=1"  for page in range(1,78)]
        headers = {
            "User-Agent":ua,
            "cookie":"buvid3=1716C962-5FF5-2F32-B58E-00C5F1AB266B18267infoc; b_nut=1730357518; _uuid=9F7863C6-F8AE-102D2-10DBA-B62218DD79AE15333infoc; enable_web_push=DISABLE; buvid_fp=3f794d405256f0fedbdb986e1b9e0626; buvid4=A106AAE2-F169-DE59-D2C3-13D3E8280B0F20374-024103106-RO8rml%2BR%2BvPyBcZmPHklJw%3D%3D; rpdid=|(k|~RuRu||k0J'u~J|Jl~lkl; header_theme_version=CLOSE; is-2022-channel=1; enable_feed_channel=ENABLE; CURRENT_FNVAL=4048; bp_t_offset_1779294576=1054507513044533248; CURRENT_QUALITY=112; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDQ4NTA1NDIsImlhdCI6MTc0NDU5MTI4MiwicGx0IjotMX0.IheIFPTe1MOh37ExTLq5x25X25ozIiL79nzaBpPuX14; bili_ticket_expires=1744850482; browser_resolution=1392-866; home_feed_column=4; sid=6r8yt366; b_lsid=210C282E7_1963435D4B2"
        }
        for url in urls:
            yield scrapy.Request(url,headers = headers)

    def parse(self, response):
        dramas = json.loads(response.text)["data"]["list"]
        for drams in dramas:
            title = drams["title"]
            link = drams["link"]
            yield scrapy.Request(link, callback = self.content)

    def content(self,response):
        urls = response
        headers = {
            "User-Agent": ua,
            "cookie": "buvid3=1716C962-5FF5-2F32-B58E-00C5F1AB266B18267infoc; b_nut=1730357518; _uuid=9F7863C6-F8AE-102D2-10DBA-B62218DD79AE15333infoc; enable_web_push=DISABLE; buvid_fp=3f794d405256f0fedbdb986e1b9e0626; buvid4=A106AAE2-F169-DE59-D2C3-13D3E8280B0F20374-024103106-RO8rml%2BR%2BvPyBcZmPHklJw%3D%3D; rpdid=|(k|~RuRu||k0J'u~J|Jl~lkl; header_theme_version=CLOSE; is-2022-channel=1; enable_feed_channel=ENABLE; CURRENT_FNVAL=4048; bp_t_offset_1779294576=1054507513044533248; CURRENT_QUALITY=112; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDQ4NTA1NDIsImlhdCI6MTc0NDU5MTI4MiwicGx0IjotMX0.IheIFPTe1MOh37ExTLq5x25X25ozIiL79nzaBpPuX14; bili_ticket_expires=1744850482; browser_resolution=1392-866; home_feed_column=4; sid=6r8yt366; b_lsid=210C282E7_1963435D4B2"
        }

